//
//  NewsTableView.swift
//  SwiftNews
//
//  Created by admin on 15/10/22.
//  Copyright © 2015年 admin. All rights reserved.
//

import UIKit

class NewsTableView: UITableView ,UITableViewDelegate, UITableViewDataSource {
    



    var headFreshControl:UIRefreshControl!//刷新控件
    
    var loadMoreFooter:HWLoadMoreFooter! //加载更多控件
    
    var mySouceMuArray:NSMutableArray!
    
    
    
    override  init(frame: CGRect, style: UITableViewStyle) {
        
       super.init(frame: frame, style: style)
    
        mySouceMuArray = NSMutableArray ()
        self.delegate=self;
        
        self.dataSource=self;
        
        self.initLoadData()
        
        self.addView()
    }
    
    //数据更新
    func initLoadData (){
    
        
        for var index = 0;index<10;index++ {
            mySouceMuArray .addObject(index)
        
        }
        
        
        self.reloadData();
        
    }
    
    
    func loadMoreStatus (){//追加更多
    
        
        self .initLoadData()
        self.tableFooterView?.hidden=true
         self.reloadData()
    }
    
    //布局UI
    func addView (){
    
        headFreshControl = UIRefreshControl ();
        headFreshControl.addTarget(self, action:"refreshStareChange:", forControlEvents: UIControlEvents.ValueChanged)
        headFreshControl.attributedTitle = NSAttributedString(string: "努力加载中......")
            self .addSubview(headFreshControl)
        
        
        headFreshControl.beginRefreshing()
        
        
        self .refreshStareChange( headFreshControl )
        
        loadMoreFooter = HWLoadMoreFooter (frame: CGRectMake(0, 0, self.get_view_width(self), 40) );
        loadMoreFooter.backgroundColor=UIColor.orangeColor()
        self.tableFooterView = loadMoreFooter;
        self.tableFooterView?.hidden=true
     
        
    
    }
    
    //刷新的方法
    func refreshStareChange(refreshStareChange:UIRefreshControl)
    {
        
        
        self.initLoadData()
        
        headFreshControl.endRefreshing()
        
        self.reloadData()
        
    }
    
    
    //TableVeiw 代理方法
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mySouceMuArray.count;
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
     let  cellName: String  = "CellName"
        
        var   cell  = tableView.dequeueReusableCellWithIdentifier(cellName) as UITableViewCell!
        
        if(cell == nil)
        {
            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellName) as UITableViewCell!
        
        }
        
        cell.textLabel?.text =  "你好帅\( indexPath.row+1)"
        
        
        return cell ;
        
        
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 100
    }

    

    // 滚动视图的代理方法
    
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        
        if ((self.tableFooterView?.hidden == false )||(mySouceMuArray.count == 0))
        {
            return
        }
        
        
        
       let offsetY =  scrollView .contentOffset.y
        
        let judgeOffsetY = scrollView.contentSize.height + scrollView.contentInset.bottom - scrollView.frame.size.height - 40
        
        
        
        
        if (offsetY >=  judgeOffsetY)
        {
            
            self.tableFooterView?.hidden=false
            //self.loadMoreStatus()
        
        }
        
        
    }
    
    
    //获取view对位置的款
    func get_view_width (view:UIView) -> CGFloat
    {
        return view.frame.size.width
        
    }
    
    func get_view_height (view:UIView) -> CGFloat
    {
        return view.frame.size.height
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
